package com.zimi.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zimi.model.SysOrg;
import com.zimi.model.SysOrgExtend;
import com.zimi.model.SysOrgPartner;
import com.zimi.service.SysOrgPartnerService;
import com.zimi.util.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 关系企业表 api
 *
 * @author lnintr
 * @since 2019-06-11
 */
@RestController
@RequestMapping("sys-org-partner")
public class SysOrgPartnerApi {

    @Autowired
    private SysOrgPartnerService sysOrgPartnerService;

    @PostMapping("pageList")
    public Object pageList(int orgId, int limit, int page) {
        PageHelper.startPage(page, limit);
        List<SysOrg> list = sysOrgPartnerService.getPageList(orgId);
        PageInfo<SysOrg> pageInfo = new PageInfo<>(list);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("data", list);
        map.put("count", pageInfo.getTotal());
        return map;
    }

    @PostMapping("getOgnList")
    public Object getOgnList(int orgId, int limit, int page) {
        PageHelper.startPage(page, limit);
        List<SysOrgExtend> list = sysOrgPartnerService.getPartnerOgnList(orgId);
        PageInfo<SysOrgExtend> pageInfo = new PageInfo<>(list);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("data", list);
        map.put("count", pageInfo.getTotal());
        return map;
    }

    @PostMapping("save")
    public Object save(SysOrg sysOrg, HttpServletRequest request) {
        String[] ma = request.getParameterValues("orgList");
        List<SysOrg> sysOrgList = GsonUtil.json2Objs(ma[0], SysOrg.class);

        sysOrgPartnerService.save(sysOrg.getId(), sysOrgList);

        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("del")
    public Object del(SysOrgPartner partner) {

        sysOrgPartnerService.del(partner);

        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }
}
